package com.ate.javamvp.mvp.presenter;

import com.ate.javamvp.MyObserver;
import com.ate.javamvp.mvp.model.DataModel;
import com.ate.ui.LoginActivity;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
/**
 * @author Ate
 * @description:
 * @date :2020/12/24 14:52
 */
public class LoginPresenter {
    LoginActivity loginActivity;
    private DataModel dataModel;

    public LoginPresenter(LoginActivity loginActivity) {
        this.loginActivity = loginActivity;
        dataModel = new DataModel();
    }
    Observer<JsonObject> observer2= new MyObserver<JsonObject>() {
        @Override
        public void onNext(JsonObject jsonObject) {
            try {
                JSONObject object = new JSONObject(jsonObject.toString());
                String code = object.getString("code");
                if (code.equals("200")){
                    loginActivity.LoginSucce(jsonObject);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(Throwable e) {

        }
    };
    public void login(RequestBody requestBody){
        dataModel.login(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer2);
    }
}
